//
//  SSCleanSecondaryPageController.swift
//  拾舍清洁
//
//  Created by sky.wei on 2023/2/14.
//

import UIKit
import SwiftyJSON


class SSCleanDetailController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    var headerView: SSCleanDetailHeaderView = SSCleanDetailHeaderView()
    
    ///1、日常清洁2、退房清洁3、深度清洁4、蒸汽地毯
    var typeID:Int = 1
    var items: Array<ListItemCombo> = []
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        tableView.reloadData()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if(typeID == 1){
            title = "日常清洁"
        }else if(typeID == 3){
            title = "深度清洁"
        }else if(typeID == 4){
            title = "蒸汽地毯"
        }
        
        let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
        btn.setImage(UIImage(named: "headset_nav"), for: .normal)
        btn .addTarget(self, action: #selector(btnclick), for: .touchUpInside)
        let rightItem = UIBarButtonItem(customView: btn)
        self.navigationItem.rightBarButtonItem = rightItem

        let headerView = SSCleanDetailHeaderView.headerView()
//        headerView.backgroundColor = UIColor.blue
        headerView.setTypeID(typeID: self.typeID)
        headerView.currentVC = self
        headerView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 550)
        self.headerView = headerView
        tableView.tableHeaderView = headerView
       
        
        tableView.register(UINib(nibName: "SSHomeTableViewCell", bundle: Bundle.init(for: SSHomeTableViewCell.self)), forCellReuseIdentifier: "SSHomeTableViewCell")
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        
   
        
        getListNetwork()
        if self.typeID != 4 {
            self.getSubItemNetwork()
        }
        //        self.getSubItemNetwork()
    }

    
    
    //MARK: privateAction
    
    @IBAction func shoppingcartAction(_ sender: Any) {
    }
    
    @objc func btnclick(){
//        let vc = SSQRCodeViewController.init(nibName: "SSQRCodeViewController", bundle: nil)
//        present(vc, animated: true)
        enterMeiqia()
    }
    
    //MARK: -delegate
    func addAction() {
        let vc = SSSingleMealViewController.init(nibName: "SSSingleMealViewController", bundle: nil)
        present(vc, animated: true)
    }
    
    //MARK: -networing
    //list
    func getListNetwork() {
        let param = [
            "parentId": "\(self.typeID)"
        ] as [String:AnyObject]
        SSProductRequest.getProductRequest(parameters: param) { items in
            self.items = items
            self.tableView.reloadData()
            print("")
//            if self.typeID != 4 {
//                self.getSubItemNetwork()
//            }
        }
    }
    
    func getSubItemNetwork() {
        let param = [
            "parentId": "\(self.typeID)"
        ] as [String:AnyObject]
        HttpConnectTools.share.request(urlString: APIUrl.share.getItem_URL, parameters: param) { (result, error) in
            if let _ = result{
                let json = JSON(parseJSON: result!)
                let retCode = json["retCode"].stringValue
                let retMsg = json["retMsg"].stringValue
                if(retCode == "0000"){
                    let data = json["data"]["listItem"].arrayValue
                    let items : ListItemCombo = ListItemCombo.deserialize(from: result, designatedPath: "data")!
                    debugPrint("----------",items as Any)
                    self.headerView.setItems(items: items.listItem, typeID:self.typeID)
                    print("---data\(data)")
                    self.tableView.reloadData()
                }else{
                    dissmissToTast(msg: retMsg)
                }
            }
        }
    }
    
}
    


extension SSCleanDetailController: UITableViewDelegate, UITableViewDataSource
{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//        let item :ListCombo =  self.items[indexPath.section].listCombos[indexPath.section] as! ListCombo
//        
//        let vc =  SSItemDetailViewController.init(nibName: "SSItemDetailViewController", bundle: nil)
//        vc.id = item.id
//        present(vc, animated: true)
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return self.items.count
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 42
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        var title = ""
        if self.items.count > 0 {
            title = (self.items[section] ).name
        }
        let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 42))
        view.backgroundColor = UIColor.white
        
        let label = UILabel()
        label.text = title
        label.font = UIFont.systemFont(ofSize: 18, weight: .bold)
        label.textColor = UIColor.black
        view.addSubview(label)
//        label.backgroundColor = UIColor.red
        label.snp.makeConstraints { make in
            make.width.equalTo(150)
            make.height.equalTo(26)
            make.centerY.equalTo(view.frame.size.height/2)
            make.left.equalTo(16)
        }
        
        return view
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items[section].listCombos.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SSHomeTableViewCell") as! SSHomeTableViewCell
        cell.setType(type: "\(typeID)")
        cell.setData(self.items[indexPath.section].listCombos[indexPath.row] as! ListCombo)
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 170
    }
}
